/*
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see http://www.gnu.org/licenses/. 
 *
 *  Authors:
 *  	Albert Marieges Garcia
 *  	Álex Pardo Fernández
 *  	Enric Mayas Márquez
 *  	Juan Carlos Calvo Tejedor
 *  	Marc Gironés Dezsényi
 *  
 *  Contact:
 *  	bcn.tours.android@gmail.com
 *  Webpage:
 *  	bcntours.co.cc
 *
 */
package com.BCNTours.Vista;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.BCNTours.R;
import com.BCNTours.AR.Vista.ARRoute;
import com.BCNTours.AR.Vista.ARactivity;
import com.BCNTours.Info.Vista.InfoActivity;
import com.BCNTours.Maps.Vista.MapMode;
import com.BCNTours.Maps.Vista.MapRoute;
import com.BCNTours.Model.Attraction;
import com.BCNTours.Model.InformationPoint;
import com.BCNTours.Model.Monument;
import com.BCNTours.Model.Museum;
import com.BCNTours.Model.Poi;

/**
 * Dialog that shows info of a particular POI and also shows a range of options available to it.
 *
 */
public class PoiDialog extends Dialog implements android.view.View.OnClickListener{
	
	private Context context;
	private Poi interestPoint;
    private Button btnInfo;
    private Button btnMapRoute;
    private Button btnHowToGo;
    private ARactivity arActivity;
    private MapMode map;
    
    public PoiDialog(ARactivity arActivity, Context context, Poi interestPoint) {
    		super(arActivity);
    		this.interestPoint = interestPoint;
    		this.context = context;
    		this.arActivity = arActivity;
    		
            setContentView(R.layout.poi_dialog);
            setTitle(interestPoint.getName());
            
            //Find the image of the dialog and add it
            ImageView picture = (ImageView) findViewById(R.id.imgDialog);
            picture.setImageResource(interestPoint.getImage2());
            
            //Find the buttons of the dialog and add the action listener
            btnInfo = (Button) findViewById(R.id.btnInfo);
            btnInfo.setOnClickListener(this);
            btnMapRoute = (Button) findViewById(R.id.btnMapRoute);
            btnMapRoute.setOnClickListener(this);
            btnHowToGo = (Button) findViewById(R.id.btnHowToGo);
            btnHowToGo.setOnClickListener(this);   
   	}
    
    public PoiDialog(MapMode map, Context context, Poi interestPoint) {
		super(map);
		this.interestPoint = interestPoint;
		this.context = context;
		this.map = map;
		
        setContentView(R.layout.poi_dialog);
        setTitle(interestPoint.getName());
        
        //Find the image of the dialog and add it
        ImageView picture = (ImageView) findViewById(R.id.imgDialog);
        picture.setImageResource(interestPoint.getImage2());
        
        //Find the buttons of the dialog and add the action listener
        btnInfo = (Button) findViewById(R.id.btnInfo);
        btnInfo.setOnClickListener(this);
        btnMapRoute = (Button) findViewById(R.id.btnMapRoute);
        btnMapRoute.setOnClickListener(this);
        btnHowToGo = (Button) findViewById(R.id.btnHowToGo);
        btnHowToGo.setOnClickListener(this);    
}

    /**
     * Action listener of the dialogs buttons
     * @param v
     */
    public void onClick(View v) {
    	Intent intent = null;
    	ConnectivityManager cm;
		switch(v.getId()){
			//Info button
			case R.id.btnInfo:
				
				//Change the intent to Info
				intent = new Intent(context, InfoActivity.class);
				//Analyze the type of the interest point
		    	if(interestPoint instanceof Attraction){
		    		Attraction att = (Attraction) interestPoint;
					intent.putExtra("com.ShowMe.Model.Attraction", att);
					intent.putExtra("tipo", 0);
		    	} else if(interestPoint instanceof Monument){
		    		Monument mon = (Monument) interestPoint;
					intent.putExtra("com.ShowMe.Model.Monument", mon);
					intent.putExtra("tipo", 1);
		    	} else if(interestPoint instanceof Museum){
		    		Museum mus = (Museum) interestPoint;
					intent.putExtra("com.ShowMe.Model.Museum", mus);
					intent.putExtra("tipo", 2);
		    	} else{
		    		InformationPoint inf = (InformationPoint) interestPoint;
					intent.putExtra("com.ShowMe.Model.InformationPoint", inf);
					intent.putExtra("tipo", 3);
		    	}
		    	break;
            	
            //MapRoute button
			case R.id.btnMapRoute:
				//Change the intent to HowToGo
	    		cm = (ConnectivityManager) getContext().
	    							getSystemService( Context.CONNECTIVITY_SERVICE );
	    		
				if(cm.getActiveNetworkInfo() != null){
		    		intent = new Intent(context, MapRoute.class);
		    		intent.putExtra("lat", interestPoint.getGpsLat()* 1000000);
		    		intent.putExtra("long", interestPoint.getGpsLong()* 1000000);
		    		intent.putExtra("nom", interestPoint.getName());
				}else{
					
					Toast.makeText(getContext(),getContext().getString(R.string.no_internet),
							Toast.LENGTH_LONG).show();
				}
	    		break;
				
			//HowToGo button
			case R.id.btnHowToGo:
				cm = (ConnectivityManager) getContext().
								getSystemService( Context.CONNECTIVITY_SERVICE );

				if(cm.getActiveNetworkInfo() != null){
					//Change the intent to ARHowToGo
		    		intent = new Intent(context, ARRoute.class);
		    		intent.putExtra("latitude", new Float(interestPoint.getGpsLat()));
		    		intent.putExtra("longitude", new Float(interestPoint.getGpsLong()));
				}else{
					
					Toast.makeText(getContext(),getContext().getString(R.string.no_internet),
							Toast.LENGTH_LONG).show();
				}
		    		
				break;
		}
		
		//Change the intent
		if(arActivity != null){	
			if(intent != null){
				arActivity.startActivity(intent);
			}
		}else if(intent != null){
			map.startActivity(intent);	
		}
    }
    
    
}